import 'package:flutter/material.dart';

class CustomRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("自定义组件"),
      ),
      body: Column(
        children: <Widget>[
          CustomPaint(
            size: Size(300, 600),
            painter: new MyCustomPainter(),
          ),
        ],
      ),
    );
  }
}

class MyCustomPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    const PI = 3.1415926;
    //级联表达式
    var paint = Paint()
      ..color = Colors.black
      ..style = PaintingStyle.fill
      ..isAntiAlias = true;
    //绘制矩形
    canvas.drawRect(Rect.fromLTRB(10, 10, 300, 50), paint..color = Colors.blue);
    //绘制圆
    canvas.drawCircle(Offset(150, 100), 40, paint..color = Colors.redAccent);
    //绘制扇形
    canvas.drawArc(Rect.fromCircle(center: Offset(150, 150), radius: 100), 0,
        90 / 180 * PI, true, paint);
    //绘制椭圆
    canvas.drawOval(
        Rect.fromCenter(center: Offset(150, 310), width: 200, height: 100),
        paint..color = Colors.green);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}
